/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.tms.web;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import com.jeeplus.common.sortList.SortList;
import com.jeeplus.common.utils.excel.ObjectExcelView;
import com.jeeplus.common.utils.excel.PageData;
import com.jeeplus.modules.tms.entity.LineReceive;
import com.jeeplus.modules.tms.entity.LineSend;
import com.jeeplus.modules.tms.entity.Resource;
import com.jeeplus.modules.tms.importEntity.SpecialLineImport;
import com.jeeplus.modules.tms.mapper.LineReceiveMapper;
import com.jeeplus.modules.tms.mapper.LineSendMapper;
import com.jeeplus.modules.tms.service.ResourceService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.tms.entity.SpecialLine;
import com.jeeplus.modules.tms.service.SpecialLineService;

/**
 * 专线Controller
 *
 * @author dhw
 * @version 2018-01-16
 */
@Controller
@RequestMapping(value = "${adminPath}/tms/specialLine")
public class SpecialLineController extends BaseController {

    @Autowired
    private SpecialLineService specialLineService;
    @Autowired
    private LineReceiveMapper lineReceiveMapper;
    @Autowired
    private LineSendMapper lineSendMapper;
    @Autowired
    private ResourceService resourseService;

    @ModelAttribute
    public SpecialLine get(@RequestParam(required = false) String id) {
        SpecialLine entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = specialLineService.get(id);
        }
        if (entity == null) {
            entity = new SpecialLine();
        }
        return entity;
    }

    /**
     * 专线列表页面
     */
    @RequiresPermissions("tms:specialLine:list")
    @RequestMapping(value = {"list", ""})
    public String list() {
        //删除两个子表空白数据（人、地址、电话全部为空时，删除）
        LineSend lineSendnew = new LineSend();
        LineReceive lineReceivenew = new LineReceive();
        List<LineSend> lineSendList = lineSendMapper.findList(lineSendnew);
        if (lineSendList.size() > 0) {
            for (LineSend lineSend1 : lineSendList) {
                if ((StringUtils.isEmpty(lineSend1.getAddress())) && (StringUtils.isEmpty(lineSend1.getContacts())) && (StringUtils.isEmpty(lineSend1.getPhone()))) {
                    lineSendMapper.delete(lineSend1);
                }
            }
        }
        List<LineReceive> lineReceiveList = lineReceiveMapper.findList(lineReceivenew);
        if (lineReceiveList.size() > 0) {
            for (LineReceive lineReceive1 : lineReceiveList) {
                if ((StringUtils.isEmpty(lineReceive1.getAddress())) && (StringUtils.isEmpty(lineReceive1.getContacts())) && (StringUtils.isEmpty(lineReceive1.getPhone()))) {
                    lineReceiveMapper.delete(lineReceive1);
                }
            }
        }

        SpecialLine specialLine = new SpecialLine();
        List<SpecialLine> truckOwners = specialLineService.findList(specialLine);
        for (SpecialLine specialLine1 : truckOwners) {
            LineReceive lineReceive = new LineReceive();
            LineSend lineSend = new LineSend();

            //判断两个子表是否重复（依据地址、电话、人全部相同时判断重复）
            boolean send = false;
            boolean receive = false;

            lineReceive.setSpecialLine(specialLine1);
            List<LineReceive> lineReceives = lineReceiveMapper.findList(lineReceive);
            if (lineReceives.size() > 0) {
                for (int i = 0; i < lineReceives.size(); i++) {
                    for (int j = 1; j > i && j < lineReceives.size(); j++) {
                        if (lineReceives.get(i).getAddress()==null||(lineReceives.get(j).getAddress())==null ||lineReceives.get(i).getAddress().equals(lineReceives.get(j).getAddress())) {
                            if (lineReceives.get(i).getContacts()==null||(lineReceives.get(j).getContacts()==null)||lineReceives.get(i).getContacts().equals(lineReceives.get(j).getContacts())) {
                                if ((lineReceives.get(i).getPhone()==null||(lineReceives.get(j).getPhone()==null))||(lineReceives.get(i).getPhone().equals(lineReceives.get(j).getPhone()))) {
                                    receive = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (receive) {
                        break;
                    }
                }
            }

            lineSend.setSpecialLine(specialLine1);
            List<LineSend> lineSends = lineSendMapper.findList(lineSend);
            if (lineSends.size() > 0) {
                for (int i = 0; i < lineSends.size(); i++) {
                    for (int j = 1; j > i && j < lineSends.size(); j++) {
                        if (lineSends.get(i).getAddress()==null||(lineSends.get(j).getAddress())==null ||lineSends.get(i).getAddress().equals(lineSends.get(j).getAddress())) {
                            if (lineSends.get(i).getContacts()==null||(lineSends.get(j).getContacts()==null)||lineSends.get(i).getContacts().equals(lineSends.get(j).getContacts())) {
                                if ((lineSends.get(i).getPhone()==null||(lineSends.get(j).getPhone()==null))||(lineSends.get(i).getPhone().equals(lineSends.get(j).getPhone()))) {
                                    send = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (send) {
                        break;
                    }
                }
            }
            if (send || receive) {
                specialLine1.setIsrepeat("1");
            } else {
                specialLine1.setIsrepeat("0");
            }

            specialLineService.save(specialLine1);
        }
        return "modules/tms/specialLineList";
    }

    /**
     * 专线列表数据
     */
    @ResponseBody
    @RequiresPermissions("tms:specialLine:list")
    @RequestMapping(value = "data")
    public Map<String, Object> data(SpecialLine specialLine, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<SpecialLine> page = specialLineService.findPage(new Page<SpecialLine>(request, response), specialLine);
        return getBootstrapData(page);
    }

    /**
     * 查看，增加，编辑专线表单页面
     */
    @RequiresPermissions(value = {"tms:specialLine:view", "tms:specialLine:add", "tms:specialLine:edit"}, logical = Logical.OR)
    @RequestMapping(value = "form")
    public String form(SpecialLine specialLine, Model model) {
        model.addAttribute("specialLine", specialLine);
        return "modules/tms/specialLineForm";
    }

    /**
     * 保存专线
     */
    @ResponseBody
    @RequiresPermissions(value = {"tms:specialLine:add", "tms:specialLine:edit"}, logical = Logical.OR)
    @RequestMapping(value = "save")
    public AjaxJson save(SpecialLine specialLine, Model model, RedirectAttributes redirectAttributes) throws Exception {
        AjaxJson j = new AjaxJson();
        if (!beanValidator(model, specialLine)) {
            j.setSuccess(false);
            j.setMsg("非法参数！");
            return j;
        }
        //新增或编辑表单保存
        List<SpecialLine> specialLineList = specialLineService.findList(specialLine);
        if (specialLineList.size() == 0) {
            specialLineService.save(specialLine);//保存
            j.setSuccess(true);
            j.setMsg("保存专线成功");
            return j;
        } else {
            int i = 1;
            for (SpecialLine specialL : specialLineList) {
                if ((specialL.getStartStation()).equals((specialLine.getStartStation())) && (specialL.getEndStation()).equals((specialLine.getEndStation())) && (specialL.getResource()).equals((specialLine.getResource()))) {
                    i++;
                }
            }
            if (!specialLine.getIsNewRecord()) {
                int k = 1;
                for (SpecialLine specialL : specialLineList) {
                    if ((specialL.getStartStation()).equals((specialLine.getStartStation())) && (specialL.getEndStation()).equals((specialLine.getEndStation())) && (specialL.getResource()).equals((specialLine.getResource())) && !(((specialL.getId()).equals(specialLine.getId())))) {
                        k++;
                    }
                }
                if (k == 1) {
                    specialLineService.save(specialLine);//编辑保存
                    j.setSuccess(true);
                    j.setMsg("专线保存成功");
                } else {
                    j.setSuccess(false);
                    j.setMsg("专线已存在!");
                }
            } else {
                if (i == 1) {
                    specialLineService.save(specialLine);//编辑保存
                    j.setSuccess(true);
                    j.setMsg("保存专线成功");
                } else {
                    j.setSuccess(false);
                    j.setMsg("专线已存在!");
                }
            }
        }
        return j;
    }

    /**
     * 删除专线
     */
    @ResponseBody
    @RequiresPermissions("tms:specialLine:del")
    @RequestMapping(value = "delete")
    public AjaxJson delete(SpecialLine specialLine, RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        specialLineService.delete(specialLine);
        j.setMsg("删除专线成功");
        return j;
    }

    /**
     * 批量删除专线
     */
    @ResponseBody
    @RequiresPermissions("tms:specialLine:del")
    @RequestMapping(value = "deleteAll")
    public AjaxJson deleteAll(String ids, RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        String idArray[] = ids.split(",");
        for (String id : idArray) {
            specialLineService.delete(specialLineService.get(id));
        }
        j.setMsg("删除专线成功");
        return j;
    }

    /**
     * 导出excel文件
     */
    @ResponseBody
    @RequiresPermissions("tms:specialLine:export")
    @RequestMapping(value = "export", method = RequestMethod.POST)
    public AjaxJson exportFile(SpecialLine specialLine, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        try {
            String fileName = "专线" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            Page<SpecialLine> page = specialLineService.findPage(new Page<SpecialLine>(request, response, -1), specialLine);
            new ExportExcel("专线", SpecialLine.class).setDataList(page.getList()).write(response, fileName).dispose();
            j.setSuccess(true);
            j.setMsg("导出成功！");
            return j;
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导出专线记录失败！失败信息：" + e.getMessage());
        }
        return j;
    }

    @ResponseBody
    @RequestMapping(value = "detail")
    public SpecialLine detail(String id) {
        return specialLineService.get(id);
    }


    /**
     * 导入Excel数据
     */
    @RequiresPermissions("tms:specialLine:import")
    @RequestMapping(value = "imports", method = RequestMethod.POST)
    public String importFiles(MultipartFile file, RedirectAttributes redirectAttributes) {
        try {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<SpecialLine> list = ei.getDataList(SpecialLine.class);
            for (SpecialLine specialLine : list) {
                try {
                    specialLineService.save(specialLine);
                    successNum++;
                } catch (ConstraintViolationException ex) {
                    failureNum++;
                } catch (Exception ex) {
                    failureNum++;
                }
            }
            if (failureNum > 0) {
                failureMsg.insert(0, "，失败 " + failureNum + " 条专线记录。");
            }
            addMessage(redirectAttributes, "已成功导入 " + successNum + " 条专线记录" + failureMsg);
        } catch (Exception e) {
            addMessage(redirectAttributes, "导入专线失败！失败信息：" + e.getMessage());
        }
        return "redirect:" + Global.getAdminPath() + "/tms/specialLine/?repage";
    }

    /**
     * 下载导入专线数据模板
     */
    @RequiresPermissions("tms:specialLine:import")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
        try {
            String fileName = "专线数据导入模板.xlsx";
            List<SpecialLine> list = Lists.newArrayList();
            new ExportExcel("专线数据", SpecialLine.class, 1).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导入模板下载失败！失败信息：" + e.getMessage());
        }
        return "redirect:" + Global.getAdminPath() + "/tms/specialLine/?repage";
    }

    /**
     * 专线导出数据
     */
    @RequestMapping(value = "exportspecial")
    public ModelAndView export(SpecialLine specialLine, String ids, HttpServletRequest request, HttpServletResponse response) {

        List<SpecialLine> specialLineList = new ArrayList<SpecialLine>();

        if (StringUtils.isNotEmpty(ids)) {
            String idArray[] = ids.split(",");
            for (String id : idArray) {
                specialLineList.add(specialLineService.get(id));
            }
        } else {
            specialLineList = specialLineService.findList(specialLine);
        }

        try {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            List<String> titles = new ArrayList<String>();
            //添加订单签收标题
            titles.add("公司名称");
            titles.add("发站");
            titles.add("到站");
            titles.add("到站归属地");
            titles.add("省（直辖市）全境覆盖");
            titles.add("重货（元/吨）");
            titles.add("泡货（元/M³）");
            titles.add("运输时效");
            titles.add("专线负责人");
            titles.add("专线负责人电话");
            titles.add("免费提货量");
            titles.add("免费送货量");
            titles.add("是否精品专线");
            titles.add("精品专线到期日期");
            titles.add("是否置顶");
            titles.add("置顶到期日期");
            titles.add("录入日期");
            titles.add("修改日期");
            titles.add("收货地址");
            titles.add("联系人");
            titles.add("电话");
            titles.add("提货地址");
            titles.add("联系人");
            titles.add("电话");
            dataMap.put("titles", titles);

            List<PageData> excelList = new ArrayList<PageData>();
            //添加表内容
            for (SpecialLine yq : specialLineList) {

                List<LineSend> lineSendList = new ArrayList<>();//提货
                List<LineReceive> lineReceiveList = new ArrayList<>();//收货

                LineSend lineSend = new LineSend();
                LineReceive lineReceive = new LineReceive();
                List<LineSend> lineSendLists = lineSendMapper.findList(lineSend);
                for (LineSend lineSends : lineSendLists) {
                    if ((lineSends.getSpecialLine().getId()).equals(yq.getId())) {
                        lineSendList.add(lineSends);
                    }
                }
                List<LineReceive> lineReceiveLists = lineReceiveMapper.findList(lineReceive);
                for (LineReceive lineReceives : lineReceiveLists) {
                    if ((lineReceives.getSpecialLine().getId()).equals(yq.getId())) {
                        lineReceiveList.add(lineReceives);
                    }
                }


                int i = lineSendList.size() > lineReceiveList.size() ? lineSendList.size() : lineReceiveList.size();
                if (i > 0) {
                    for (int j = 0; j < i; j++) {

                        PageData map = new PageData();
                        map.put("var1", yq.getResource().getName());
                        map.put("var2", yq.getStartStation());
                        if (StringUtils.isNotEmpty(yq.getEndStation())) {
                            map.put("var3", yq.getEndStation());
                        } else {
                            map.put("var3", "");
                        }
                        if (StringUtils.isNotEmpty(yq.getEndArea())) {
                            map.put("var4", yq.getEndArea());
                        } else {
                            map.put("var4", "");
                        }
                        if (StringUtils.isNotEmpty(yq.getCover())) {
                            map.put("var5", yq.getCover());
                        } else {
                            map.put("var5", "");
                        }
                        if ((yq.getTonPrice()) != null) {
                            map.put("var6", String.valueOf(yq.getTonPrice()));
                        } else {
                            map.put("var6", "");
                        }
                        if ((yq.getSterePrice()) != null) {
                            map.put("var7", String.valueOf(yq.getSterePrice()));
                        } else {
                            map.put("var7", "");
                        }
                        if (StringUtils.isNotEmpty(yq.getTransportationPrescription())) {
                            map.put("var8", yq.getTransportationPrescription());
                        } else {
                            map.put("var8", "");
                        }
                        if (StringUtils.isNotEmpty(yq.getContacts())) {
                            map.put("var9", yq.getContacts());
                        } else {
                            map.put("var9", "");
                        }
                        if (StringUtils.isNotEmpty(yq.getContactsPhone())) {
                            map.put("var10", yq.getContactsPhone());
                        } else {
                            map.put("var10", "");
                        }
                        if (StringUtils.isNotEmpty(yq.getFreePick())) {
                            if ("0".equals(yq.getFreePick())) {
                                map.put("var11", "3吨/10方");
                            }
                            if ("1".equals(yq.getFreePick())) {
                                map.put("var11", "4吨13.5方");
                            }
                            if ("2".equals(yq.getFreePick())) {
                                map.put("var11", "5吨/16.5方");
                            }
                            if ("3".equals(yq.getFreePick())) {
                                map.put("var11", "6吨/20方");
                            }
                            if ("4".equals(yq.getFreePick())) {
                                map.put("var11", "免起送量（都免费提送）");
                            }
                        } else {
                            map.put("var11", "");
                        }
                        if (StringUtils.isNotEmpty(yq.getFreeSend())) {
                            if ("0".equals(yq.getFreeSend())) {
                                map.put("var11", "3吨/10方");
                            }
                            if ("1".equals(yq.getFreeSend())) {
                                map.put("var11", "4吨13.5方");
                            }
                            if ("2".equals(yq.getFreeSend())) {
                                map.put("var11", "5吨/16.5方");
                            }
                            if ("3".equals(yq.getFreeSend())) {
                                map.put("var11", "6吨/20方");
                            }
                            if ("4".equals(yq.getFreeSend())) {
                                map.put("var11", "免起送量（都免费提送）");
                            }
                        } else {
                            map.put("var12", "");
                        }
                        if (StringUtils.isNotEmpty(yq.getIsBoutique())) {
                            map.put("var13", yq.getIsBoutique());
                        } else {
                            map.put("var13", "");
                        }
                        if ((yq.getBoutiqueDate()) != null) {
                            map.put("var14", DateUtils.formatDate(yq.getBoutiqueDate(), "yyyy-MM-dd HH:mm:ss "));
                        } else {
                            map.put("var14", "");
                        }
                        if (StringUtils.isNotEmpty(yq.getTop())) {
                            if (yq.getTop().equals("0")) {
                                map.put("var15", "否");
                            }
                            if (yq.getTop().equals("1")) {
                                map.put("var15", "是");
                            }
                        } else {
                            map.put("var15", "");
                        }
                        if ((yq.getTopDate()) != null) {
                            map.put("var16", DateUtils.formatDate(yq.getTopDate(), "yyyy-MM-dd HH:mm:ss "));
                        } else {
                            map.put("var16", "");
                        }
                        if ((yq.getCreateDate()) != null) {
                            map.put("var17", DateUtils.formatDate(yq.getCreateDate(), "yyyy-MM-dd HH:mm:ss "));
                        } else {
                            map.put("var17", "");
                        }
                        if ((yq.getUpdateDate()) != null) {
                            map.put("var18", DateUtils.formatDate(yq.getUpdateDate(), "yyyy-MM-dd HH:mm:ss "));
                        } else {
                            map.put("var18", "");
                        }
                        if (lineSendList.size() > j) {
                            map.put("var19", lineSendList.get(j).getAddress());
                        } else {
                            map.put("var19", "");
                        }
                        if (lineSendList.size() > j) {
                            map.put("var20", lineSendList.get(j).getContacts());
                        } else {
                            map.put("var20", "");
                        }
                        if (lineSendList.size() > j) {
                            map.put("var21", lineSendList.get(j).getPhone());
                        } else {
                            map.put("var21", "");
                        }
                        if (lineReceiveList.size() > j) {
                            map.put("var22", lineReceiveList.get(j).getAddress());
                        } else {
                            map.put("var22", "");
                        }
                        if (lineReceiveList.size() > j) {
                            map.put("var23", lineReceiveList.get(j).getContacts());
                        } else {
                            map.put("var23", "");
                        }
                        if (lineReceiveList.size() > j) {
                            map.put("var24", lineReceiveList.get(j).getPhone());
                        } else {
                            map.put("var24", "");
                        }
                        excelList.add(map);
                    }
                } else {
                    PageData map = new PageData();
                    map.put("var1", yq.getResource().getName());
                    map.put("var2", yq.getStartStation());
                    if (StringUtils.isNotEmpty(yq.getEndStation())) {
                        map.put("var3", yq.getEndStation());
                    } else {
                        map.put("var3", "");
                    }
                    if (StringUtils.isNotEmpty(yq.getEndArea())) {
                        map.put("var4", yq.getEndArea());
                    } else {
                        map.put("var4", "");
                    }
                    if (StringUtils.isNotEmpty(yq.getCover())) {
                        map.put("var5", yq.getCover());
                    } else {
                        map.put("var5", "");
                    }
                    if ((yq.getTonPrice()) != null) {
                        map.put("var6", String.valueOf(yq.getTonPrice()));
                    } else {
                        map.put("var6", "");
                    }
                    if ((yq.getSterePrice()) != null) {
                        map.put("var7", String.valueOf(yq.getSterePrice()));
                    } else {
                        map.put("var7", "");
                    }
                    if (StringUtils.isNotEmpty(yq.getTransportationPrescription())) {
                        map.put("var8", yq.getTransportationPrescription());
                    } else {
                        map.put("var8", "");
                    }
                    if (StringUtils.isNotEmpty(yq.getContacts())) {
                        map.put("var9", yq.getContacts());
                    } else {
                        map.put("var9", "");
                    }
                    if (StringUtils.isNotEmpty(yq.getContactsPhone())) {
                        map.put("var10", yq.getContactsPhone());
                    } else {
                        map.put("var10", "");
                    }
                    if (StringUtils.isNotEmpty(yq.getFreePick())) {
                        if ("0".equals(yq.getFreePick())) {
                            map.put("var11", "3吨/10方");
                        }
                        if ("1".equals(yq.getFreePick())) {
                            map.put("var11", "4吨13.5方");
                        }
                        if ("2".equals(yq.getFreePick())) {
                            map.put("var11", "5吨/16.5方");
                        }
                        if ("3".equals(yq.getFreePick())) {
                            map.put("var11", "6吨/20方");
                        }
                        if ("4".equals(yq.getFreePick())) {
                            map.put("var11", "免起送量（都免费提送）");
                        }
                    } else {
                        map.put("var11", "");
                    }
                    if (StringUtils.isNotEmpty(yq.getFreeSend())) {
                        if ("0".equals(yq.getFreeSend())) {
                            map.put("var12", "3吨/10方");
                        }
                        if ("1".equals(yq.getFreeSend())) {
                            map.put("var12", "4吨13.5方");
                        }
                        if ("2".equals(yq.getFreeSend())) {
                            map.put("var12", "5吨/16.5方");
                        }
                        if ("3".equals(yq.getFreeSend())) {
                            map.put("var12", "6吨/20方");
                        }
                        if ("4".equals(yq.getFreeSend())) {
                            map.put("var12", "免起送量（都免费提送）");
                        }
                    } else {
                        map.put("var12", "");
                    }
                    if (StringUtils.isNotEmpty(yq.getIsBoutique())) {
                        if ("0".equals(yq.getIsBoutique())) {
                            map.put("var13", "否");
                        }
                        if ("1".equals(yq.getIsBoutique())) {
                            map.put("var13", "是");
                        }
                    } else {
                        map.put("var13", "");
                    }
                    if ((yq.getBoutiqueDate()) != null) {
                        map.put("var14", DateUtils.formatDate(yq.getBoutiqueDate(), "yyyy-MM-dd HH:mm:ss "));
                    } else {
                        map.put("var14", "");
                    }
                    if (StringUtils.isNotEmpty(yq.getTop())) {
                        if (yq.getTop().equals("0")) {
                            map.put("var15", "否");
                        }
                        if (yq.getTop().equals("1")) {
                            map.put("var15", "是");
                        }
                    } else {
                        map.put("var15", "");
                    }
                    if ((yq.getTopDate()) != null) {
                        map.put("var16", DateUtils.formatDate(yq.getTopDate(), "yyyy-MM-dd HH:mm:ss "));
                    } else {
                        map.put("var16", "");
                    }
                    if ((yq.getCreateDate()) != null) {
                        map.put("var17", DateUtils.formatDate(yq.getCreateDate(), "yyyy-MM-dd HH:mm:ss "));
                    } else {
                        map.put("var17", "");
                    }
                    if ((yq.getUpdateDate()) != null) {
                        map.put("var18", DateUtils.formatDate(yq.getUpdateDate(), "yyyy-MM-dd HH:mm:ss "));
                    } else {
                        map.put("var18", "");
                    }
                    map.put("var19", "");
                    map.put("var20", "");
                    map.put("var21", "");
                    map.put("var22", "");
                    map.put("var23", "");
                    map.put("var24", "");
                    excelList.add(map);
                }
            }

            String fileName = "专线" + DateUtils.formatDate(new Date(), "yyyy-MM-dd ");
            ;
            dataMap.put("fileName", fileName);

            dataMap.put("excelList", excelList);
            System.out.println("===导出excel成功！");
            ObjectExcelView erv = new ObjectExcelView();                    //执行excel操作

            return new ModelAndView(erv, dataMap);

        } catch (Exception e) {
            System.out.println("======导出excel时出错！" + e.getMessage());
        }
        return new ModelAndView("redirect:" + Global.getAdminPath() + "/tms/storesList");
    }

    /**
     * 导入Excel数据
     */
    @RequiresPermissions("tms:dropDispatch:import")
    @RequestMapping(value = "import", method = RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        try {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<SpecialLineImport> newList = ei.getDataList(SpecialLineImport.class);
            for (SpecialLineImport ddi : newList) {
                try {
                    SpecialLine specialLine = new SpecialLine();
                    LineReceive lineReceive = new LineReceive();
                    LineSend lineSend = new LineSend();
                    if (StringUtils.isNotEmpty(ddi.getResource())) {
                        Resource resource = new Resource();
                        resource.setName(ddi.getResource());
                        List<Resource> resourseList = resourseService.findList(resource);
                        SortList<Resource> msLists = new SortList<Resource>();     //调用工具类，按照指定字段排序
                        msLists.sortByMethod(resourseList, "getCreateDate", false);
                        specialLine.setResource(resourseList.get(0));
                    }
                    if (StringUtils.isNotEmpty(ddi.getStartStation())) {
                        specialLine.setStartStation(ddi.getStartStation());
                    }
                    if (StringUtils.isNotEmpty(ddi.getEndStation())) {
                        specialLine.setEndStation(ddi.getEndStation());
                    }
                    if (StringUtils.isNotEmpty(ddi.getEndArea())) {
                        specialLine.setEndArea(ddi.getEndArea());
                    }
                    if (StringUtils.isNotEmpty(ddi.getCover())) {
                        specialLine.setCover(ddi.getCover());
                    }
                    if (StringUtils.isNotEmpty(ddi.getTonPrice())) {
                        specialLine.setTonPrice(Double.valueOf(ddi.getTonPrice()));
                    }
                    if (StringUtils.isNotEmpty(ddi.getSterePrice())) {
                        specialLine.setSterePrice(Double.valueOf(ddi.getSterePrice()));
                    }
                    if (StringUtils.isNotEmpty(ddi.getTransportationPrescription())) {
                        specialLine.setTransportationPrescription(ddi.getTransportationPrescription());
                    }
                    if (StringUtils.isNotEmpty(ddi.getContacts())) {
                        specialLine.setContacts(ddi.getContacts());
                    }
                    if (StringUtils.isNotEmpty(ddi.getContactsPhone())) {
                        specialLine.setContactsPhone(ddi.getContactsPhone());
                    }
                    if (StringUtils.isNotEmpty(ddi.getFreePick())) {
                        if ("3吨/10方".equals(ddi.getFreePick())) {
                            specialLine.setFreePick("0");
                        } else if ("4吨13.5方".equals(ddi.getFreePick())) {
                            specialLine.setFreePick("1");
                        } else if ("5吨/16.5方".equals(ddi.getFreePick())) {
                            specialLine.setFreePick("2");
                        } else if ("6吨/20方".equals(ddi.getFreePick())) {
                            specialLine.setFreePick("3");
                        } else if ("免起送量（都免费提送）".equals(ddi.getFreePick())) {
                            specialLine.setFreePick("4");
                        } else {
                            specialLine.setFreePick("");
                        }
                    }
                    if (StringUtils.isNotEmpty(ddi.getFreeSend())) {
                        if ("3吨/10方".equals(ddi.getFreeSend())) {
                            specialLine.setFreeSend("0");
                        } else if ("4吨13.5方".equals(ddi.getFreeSend())) {
                            specialLine.setFreeSend("1");
                        } else if ("5吨/16.5方".equals(ddi.getFreeSend())) {
                            specialLine.setFreeSend("2");
                        } else if ("6吨/20方".equals(ddi.getFreeSend())) {
                            specialLine.setFreeSend("3");
                        } else if ("免起送量（都免费提送）".equals(ddi.getFreeSend())) {
                            specialLine.setFreeSend("4");
                        } else {
                            specialLine.setFreeSend("");
                        }
                        specialLine.setFreeSend(ddi.getFreeSend());
                    }
                    if (StringUtils.isNotEmpty(ddi.getIsBoutique())) {
                        specialLine.setIsBoutique(ddi.getIsBoutique());
                    }
                    if (StringUtils.isNotEmpty(ddi.getBoutiqueDate())) {
                        specialLine.setBoutiqueDate(df.parse(ddi.getBoutiqueDate()));
                    }
                    if (StringUtils.isNotEmpty(ddi.getSendAddress())) {
                        lineSend.setAddress(ddi.getSendAddress());
                    }
                    if (StringUtils.isNotEmpty(ddi.getSendContacts())) {
                        lineSend.setContacts(ddi.getSendContacts());
                    }
                    if (StringUtils.isNotEmpty(ddi.getSendPhone())) {
                        lineSend.setPhone(ddi.getSendPhone());
                    }
                    if (StringUtils.isNotEmpty(ddi.getReceiveAddress())) {
                        lineReceive.setAddress(ddi.getReceiveAddress());
                    }
                    if (StringUtils.isNotEmpty(ddi.getReceiveContacts())) {
                        lineReceive.setContacts(ddi.getReceiveContacts());
                    }
                    if (StringUtils.isNotEmpty(ddi.getReceivePhone())) {
                        lineReceive.setPhone(ddi.getReceivePhone());
                    }
                    if (StringUtils.isNotEmpty(ddi.getTop())) {
                        if (ddi.getTop().equals("否")) {
                            specialLine.setTop("0");
                        } else if (ddi.getTop().equals("是")) {
                            specialLine.setTop("1");
                        } else {
                            specialLine.setTop("");
                        }

                    }
                    if ((ddi.getTopDate()) != null) {
                        specialLine.setTopDate(df.parse(ddi.getTopDate()));
                    }
                    SpecialLine specialLine1 = new SpecialLine();
                    specialLine1.setStartStation(specialLine.getStartStation());
                    specialLine1.setEndStation(specialLine.getEndStation());
                    specialLine1.setResource(specialLine.getResource());
                    List<SpecialLine> specialLineList = specialLineService.findList(specialLine1);
                    if (specialLineList.size() > 0) {
                        lineReceive.setSpecialLine(specialLineList.get(0));
                        lineSend.setSpecialLine(specialLineList.get(0));
                        specialLineService.saveReceive(lineReceive);
                        specialLineService.saveSend(lineSend);
                    } else {
                        specialLineService.save(specialLine);
                        lineReceive.setSpecialLine(specialLine);
                        specialLineService.saveReceive(lineReceive);
                        lineSend.setSpecialLine(specialLine);
                        specialLineService.saveSend(lineSend);
                    }
                    successNum++;
                } catch (ConstraintViolationException ex) {
                    failureNum++;
                } catch (Exception ex) {
                    failureNum++;
                }
            }
            if (failureNum > 0) {
                failureMsg.insert(0, "，失败 " + failureNum + " 条订单记录。失败原因例如：导入数据出错！");
            }
            addMessage(redirectAttributes, "已成功导入 " + successNum + " 条订单记录" + failureMsg);
        } catch (Exception e) {
            addMessage(redirectAttributes, "导入订单失败！失败信息：" + e.getMessage());
        }
        return "redirect:" + Global.getAdminPath() + "/tms/specialLine/?repage";
    }

}